﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNet.JCore.Entity;
using DotNet.Collections;
using DotNet.Extensions;
using DotNet.Utility;
using DotNet.Helper;
using DotNet.Data;

namespace DotNet.JCore.Service
{
    /// <summary>
    /// 人工稽查登记服务
    /// </summary>
    public class ManualRegService
    {
        private JCoreRepository<ManualReg> repos = new JCoreRepository<ManualReg>();

        /// <summary>
        /// 是否存在指定名称的对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="name">名称</param>
        /// <returns>如果存在返回false</returns>
        public BoolMessage ExistsByName(string id)
        {
            if (repos.Exists(p => p.Id != id))
            {
                return new BoolMessage(false, $"已经存在");
            }
            return BoolMessage.True; ;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="isCreate">是否新增</param>
        public BoolMessage Save(ManualReg entity, bool isCreate)
        {
            var result = EntityHelper.ValidAsMessage(entity, "<br>");
            if (result.Failure)
            {
                return result;
            }
            //result = ExistsByName(entity.Id);
            //if (result.Failure)
            //{
            //    return result;
            //}

            if (isCreate)
            {
                repos.Insert(entity);
            }
            else
            {
                repos.UpdateExclude(entity, p => p.CreateId, p => p.CreateName, p => p.CreateTime);
            }
            return BoolMessage.True;
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="ids">主键数组</param>
        public BoolMessage Delete(string[] ids)
        {
            repos.Delete(ids);
            return BoolMessage.True;
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="id">主键</param>
        public ManualReg Get(string id)
        {
            return repos.Get(id);
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="Id">主键</param>
        public ManualReg GetByUserId(string Id)
        {
            return repos.Get(p => p.Id == Id);
        }

        /// <summary>
        /// 获取启用的简单对象集合(已排序)
        /// </summary>
        //public List<Simple> GetSimpleList()
        //{
        //    var query = repos.SQL
        //        .Where(p => p.Enabled == true)
        //        .OrderByDesc(p => p.CreateTime)
        //        .Select(p => p.Id, p => p.Name);

        //    return repos.Fetch(query).
        //        Select(p => new Simple(p.Id, p.Name, SpellHelper.GetSpell(p.Name)))
        //        .ToList();
        //}

        /// <summary>
        /// 获取启用的对象集合(已排序)
        /// </summary>
        /// <param name="name">名称关键字</param>
        /// <param name="enabled">有限</param>
        /// <param name="pointId">检查点</param>
        public List<ManualReg> GetList(string name, string Station, string CheckStatusName, string PointId, string startDate, string endDate)
        {
            var query = repos.SQL.OrderByDesc(p => p.CreateTime);
            SetQuery(query,name,Station, CheckStatusName,PointId,startDate,endDate);
            return repos.Fetch(query);
        }

        /// <summary>
        /// 获取对象分页集合
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="name">名称关键字</param>
        /// <param name="enabled">有限</param>
        /// <param name="pointId">检查点</param>
        public PageList<ManualReg> GetPageList(PaginationCondition pageCondition,
            string name,string Station, string CheckStatusName, string PointId, string startDate, string endDate)
        {
            pageCondition.SetDefaultOrder(nameof(ManualReg.CreateTime));
            var query = repos.PageQuery(pageCondition);
            SetQuery(query, name, Station, CheckStatusName, PointId, startDate,endDate);
            query.ExcludeSelect(p => p.Note);
            return repos.Page(query);
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <param name="search">数据对象</param>
        public void SetQuery(SQLQuery<ManualReg> query,string name,string Station, string CheckStatusName, string PointId, string startDate, string endDate)
        {
            if (Station.IsNotEmpty())
            {
                Station = Station.Trim();
                query.Where(p => p.Station == Station);
            }
            if (PointId.IsNotEmpty())
            {
                PointId = PointId.Trim();
                query.Where(p => p.PointId == PointId);
            }
            //"0：正常1：违规"
            if (CheckStatusName.IsNotEmpty())
            {
                CheckStatusName = CheckStatusName.Trim();
                //bool boolCheckStatus = Convert.ToBoolean(CheckStatusName.ToString());
                if (CheckStatusName == "1")
                {
                    query.Where(p => p.CheckStatusName == "违规");
                }
                if (CheckStatusName == "0")
                {
                    query.Where(p => p.CheckStatusName == "正常");
                }
            }
            DateTime startDt;
            if (startDate.IsNotEmpty() && DateTime.TryParse(startDate, out startDt))
            {
                query.Where(p => p.CreateTime >= startDt);
            }
            DateTime endDates;
            if (endDate.IsNotEmpty() && DateTime.TryParse(endDate, out endDates))
            {
                query.Where(p => p.CreateTime < endDates.AddDays(1));
            }
            if (name.IsNotEmpty())
            {
                name = name.Trim();
                query.Where(p => p.CarNumber.Contains(name)
                    || p.DriverName.Contains(name));
            }
        }
    }
}